<?php

App::uses('AppController', 'Controller');

/**
 * Conteudos Controller
 *
 * @property Conteudo $Conteudo
 * @property PaginatorComponent $Paginator
 */
class ConteudosController extends ConteudosAppController {

    public $uses = array('Conteudos.Conteudo', 'Conteudos.ConteudosCategoria', 'Usuarios.Usuario');
    public $name = 'conteudos';
    public $plugin = 'Conteudos';

    /**
     * __construct
     *
     * @var mixed
     * @var mixed
     */
    public function __construct($request = null, $response = null) {
        parent::__construct($request, $response);
        $this->set('title', __d('conteudos', 'Conteudos'));
    }

    /**
     * Components
     *
     * @var array
     */
    public $components = array('Paginator');

    /* --------------------------------------     Public    ----------------------------------------- */

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->Conteudo->recursive = 0;
        $this->set('conteudos', $this->Paginator->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->Conteudo->exists($id)) {
            throw new NotFoundException(__('Invalid conteudo'));
        }
        $options = array('conditions' => array('Conteudo.' . $this->Conteudo->primaryKey => $id));
        $this->set('conteudo', $this->Conteudo->find('first', $options));
    }

    /* --------------------------------------     Admin    ----------------------------------------- */

    /**
     * index method
     *
     * @return void
     */
    public function admin_index() {
        $this->Conteudo->recursive = 2;
        $this->set('conteudos', $this->Paginator->paginate());
    }

    /**
     * admin_view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_view($id = null) {
        if (!$this->Conteudo->exists($id)) {
            throw new NotFoundException(__('Invalid conteudo'));
        }
        $options = array('conditions' => array('Conteudo.' . $this->Conteudo->primaryKey => $id));
        $this->set('conteudo', $this->Conteudo->find('first', $options));
    }

    /**
     * admin_add method
     *
     * @return void
     */
    public function admin_add() {
        if ($this->request->is('post')) {
            $this->Conteudo->create();
            if ($this->Conteudo->save($this->request->data)) {
                $this->Session->setFlash(__('The conteudo has been saved.'), 'alert', array(
                    'plugin' => 'BoostCake',
                    'class' => 'alert-success'
                ));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The conteudo could not be saved. Please, try again.'), 'alert', array(
                    'plugin' => 'BoostCake',
                    'class' => 'alert-danger'
                ));
            }
        }
        debug($this->Conteudo);
        $usuarios = $this->Conteudo->Usuario->find('list');
        $conteudosCategorias = $this->Conteudo->ConteudosCategoria->find('list');
        $paginas = $this->Conteudo->Pagina->find('list');
        $this->set(compact('usuarios', 'conteudosCategorias', 'paginas'));
    }

    /**
     * admin_edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_edit($id = null) {
        if (!$this->Conteudo->exists($id)) {
            throw new NotFoundException(__('Invalid conteudo'));
        }
        if ($this->request->is(array('post', 'put'))) {
            if ($this->Conteudo->save($this->request->data)) {
                $this->Session->setFlash(__('The conteudo has been saved.'), 'alert', array(
                    'plugin' => 'BoostCake',
                    'class' => 'alert-success'
                ));
                return $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The conteudo could not be saved. Please, try again.'), 'alert', array(
                    'plugin' => 'BoostCake',
                    'class' => 'alert-danger'
                ));
            }
        } else {
            $options = array('conditions' => array('Conteudo.' . $this->Conteudo->primaryKey => $id));
            $this->request->data = $this->Conteudo->find('first', $options);
        }
        $conteudosCategorias = $this->Conteudo->ConteudosCategoria->find('list');
        $paginas = $this->Conteudo->Pagina->find('list');
        $this->set(compact('conteudosCategorias', 'paginas'));
        $this->set('title', 'Conteudos - Editar');
    }

    /**
     * admin_delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_delete($id = null) {
        $this->Conteudo->id = $id;
        if (!$this->Conteudo->exists()) {
            throw new NotFoundException(__('Invalid conteudo'));
        }
        $this->request->allowMethod('post', 'delete');
        if ($this->Conteudo->delete()) {
            $this->Session->setFlash(__('The conteudo has been deleted.'), 'alert', array(
                'plugin' => 'BoostCake',
                'class' => 'alert-success'
            ));
        } else {
            $this->Session->setFlash(__('The conteudo could not be deleted. Please, try again.'), 'alert', array(
                'plugin' => 'BoostCake',
                'class' => 'alert-danger'
            ));
        }
        return $this->redirect(array('action' => 'index'));
    }

}
